package com.ndp.fb.mq.service.schedulerservice.adpublish;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.mongo.api.PublishCacheMongoService;
import com.ndp.fb.mq.utils.RetryUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 *
 * 查询mongo 失败的处理
 * 进行多次查询，或者更新，如果继续失败，抛出异常
 * Created by hunk.qin on 2016/3/15.
 */
@Component
public class PublishCacheMongoServiceFailoverImpl implements PublishCacheMongoServiceFailover {

    @Reference
    private PublishCacheMongoService publishCacheMongoService;

    private static Logger logger = LoggerFactory.getLogger(PublishCacheMongoServiceFailoverImpl.class);

    @Override
    public void save(String cacheJsonStr) throws Exception {
        logger.info("--------save start------");
        try {
            RetryUtil.retry(3, 10, TimeUnit.SECONDS, () ->    publishCacheMongoService.save(cacheJsonStr));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
        logger.info("--------save end------");
    }

    @Override
    public void update(String batch_id, String cacheJsonStr) {
        logger.info("--------update start------");
        try {
            RetryUtil.retry(3, 10, TimeUnit.SECONDS, () -> publishCacheMongoService.update(batch_id,cacheJsonStr));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("--------update end------");
    }

    @Override
    public String getCache(String batch_Id) {
        logger.info("--------getCache start------");
        final String[] cachestr = new String[1];
        try {
            RetryUtil.retry(3, 10, TimeUnit.SECONDS, () ->  cachestr[0] = publishCacheMongoService.getCache(batch_Id));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("--------getCache end------");
        return cachestr[0];
    }

    @Override
    public void updateStatus(Long batchId, String key, int status, int type) {
        logger.info("--------updateStatus start------");
        try {
            RetryUtil.retry(3, 10, TimeUnit.SECONDS, () -> publishCacheMongoService.updateStatus( batchId, key, status, type));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("--------updateStatus end------");
    }

    @Override
    public void fillFbId(Long batchId, String key, String id, int type) {
        logger.info("--------fillFbId start------");
        try {
            RetryUtil.retry(3, 10, TimeUnit.SECONDS, () -> publishCacheMongoService.fillFbId( batchId, key, id, type));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("--------fillFbId end------");
    }

    @Override
    public String findCacheBlockByType(Long batchId, int type) {
        logger.info("--------findCacheBlockByType start------");
        final String[] cacheBlockstr = new String[1];
        try {
            RetryUtil.retry(3, 10, TimeUnit.SECONDS, () -> cacheBlockstr[0]=publishCacheMongoService.findCacheBlockByType( batchId, type));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("--------findCacheBlockByType end------");
        return cacheBlockstr[0];
    }

    @Override
    public String findCacheBlockByTypeAndKey(Long batchId, int type, String key) {
        logger.info("--------findCacheBlockByTypeAndKey start------");
        final String[] cacheBlockstr = new String[1];
        try {
            RetryUtil.retry(3, 10, TimeUnit.SECONDS, () -> cacheBlockstr[0]=publishCacheMongoService.findCacheBlockByTypeAndKey( batchId, type,key));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("--------findCacheBlockByTypeAndKey end------");
        return cacheBlockstr[0];
    }

    @Override
    public Integer findAdCreativeSuccessByCacheId(Long batchId) {
        logger.info("--------findAdCreativeSuccessByCacheId start------");
        final Integer[] succeedId = new Integer[1];
        try {
            RetryUtil.retry(3, 10, TimeUnit.SECONDS, () -> succeedId[0]=publishCacheMongoService.findAdCreativeSuccessByCacheId( batchId));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("--------findAdCreativeSuccessByCacheId end------");
        return succeedId[0];
    }

    @Override
    public void updateCreativeVideoIdByKkey(String batch_Id, String creativeKey, String videoId) {
        logger.info("--------updateCreativeVideoIdByKkey start------");
        try {
            RetryUtil.retry(3, 10, TimeUnit.SECONDS, () -> publishCacheMongoService.updateCreativeVideoIdByKkey( batch_Id, creativeKey, videoId));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("--------updateCreativeVideoIdByKkey end------");
    }

    @Override
    public void updateAdCreativeSuccessByCacheId(Long batchId, Integer adCreativeSucess) {
        logger.info("--------updateAdCreativeSuccessByCacheId start------");
        try {
            RetryUtil.retry(3, 10, TimeUnit.SECONDS, () -> publishCacheMongoService.updateAdCreativeSuccessByCacheId( batchId, adCreativeSucess));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("--------updateAdCreativeSuccessByCacheId end------");
    }

    @Override
    public String findDPAAttribute(Long batchId) {
        logger.info("--------findCatalogs start------");
        final String[] cacheBlockstr = new String[1];
        try {
            RetryUtil.retry(3, 10, TimeUnit.SECONDS, () -> cacheBlockstr[0]=publishCacheMongoService.findDPAAttribute( batchId));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        logger.info("--------findCatalogs end------");
        return cacheBlockstr[0];
    }

	@Override
	public String findMarketTargetAttribute(Long batchId) {
		 logger.info("--------findMarketTargetAttribute start------");
	        final String[] cacheBlockstr = new String[1];
	        try {
	            RetryUtil.retry(3, 10, TimeUnit.SECONDS, () -> cacheBlockstr[0]=publishCacheMongoService.findMarketTargetAttribute( batchId));
	        } catch (Exception e) {
	            logger.error(e.getMessage(), e);
	        }
	        logger.info("--------findMarketTargetAttribute end------");
	        return cacheBlockstr[0];
	}
}
